﻿#region Copyright Info

// 
// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System.Collections.Generic;
using System.IO;
using Majesty_of_Omega.Logic.GameConfiguration;
using Majesty_of_Omega.Model.Space;
using Majesty_of_Omega.Model.Game;
using Majesty_of_Omega.Logic.Generator;
using Majesty_of_Omega.Util;
using Majesty_of_Omega.Config;
#if NUNIT
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute;
#elif MBUNIT
using MbUnit.Framework;
using TestClass = MbUnit.Framework.TestFixtureAttribute;
using TestMethod = MbUnit.Framework.TestAttribute;
using TestInitialize = MbUnit.Framework.SetUpAttribute;
using TestCleanup = MbUnit.Framework.TearDownAttribute;
using ClassInitialize = MbUnit.Framework.TestFixtureSetUpAttribute;
using ClassCleanup = MbUnit.Framework.TestFixtureTearDownAttribute;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif

using Test.Majesty_of_Omega.TestUtils;

#endregion

namespace Test.Majesty_of_Omega
{
    /// <summary>
    /// This is a test class for SaveGameManager and is intended
    /// to contain all SaveGameManager Unit Tests
    /// </summary>
    [TestClass]
    public class TestGameManager
    {
        #region Helper and Private Methods


        #endregion

        #region Additional test attributes

        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #endregion

        /// <summary>
        /// Tests the saving of a star.
        /// </summary>
        [TestMethod]
        public void TestSaveStar()
        {
            SerializeHelper.SaveLoadAndCompare(new Star("Test"), "Error in saving and loading a star");
        }

        /// <summary>
        /// Tests the save star with planet.
        /// </summary>
        [TestMethod]
        public void TestSaveStarWithPlanet()
        {
            var star = new Star("Test2");
            var planet = new Planet(star);
            star.AddOrbital(1, planet);
            SerializeHelper.SaveLoadAndCompare(star, "Error in saving and loading a star with a planet");
        }

        /// <summary>
        /// Tests the save region.
        /// </summary>
        [TestMethod]
        public void TestSaveRegion()
        {
            var region = TestDataGenerator.CreateTestRegion(RegionType.Broken);
            SerializeHelper.SaveLoadAndCompare(region, "Error in saving and loading a region");
        }

        /// <summary>
        /// Tests the player.
        /// </summary>
        [TestMethod]
        public void TestPlayer()
        {
            var player = new Player("Test Empire", SpreadSheetManager.RaceModifiers.Races[0]);
            SerializeHelper.SaveLoadAndCompare(player, "Error in saving and loading a player");
        }


        /// <summary>
        ///A test for the Save method
        ///</summary>
        [TestMethod]
        public void TestSaveGame()
        {
            var testGame = TestDataGenerator.TestSaveGame;
            SerializeHelper.SaveLoadAndCompare(testGame, "error in Saving and loading a game");
        }

        /// <summary>
        ///A test for MooGameFolder
        ///</summary>
        [TestMethod]
        public void TestMooGameFolder()
        {
            var gameManager = new SaveGameManager();
            var folder = gameManager.MooGameFolder;
            Assert.IsTrue(Directory.Exists(folder), "The majesty of omega game directory must exist");
        }

        /// <summary>
        ///A test for LastGamesInMooGameFolder
        ///</summary>
        [TestMethod]
        public void TestLastGames()
        {
            // Intialize the game manager in test mode
            var gameManager = new SaveGameManager(true);
            gameManager.EraseAllGamesInMooGameFolder();
            CreateTestGameAndSaveIt("Test Game 1");
            CreateTestGameAndSaveIt("Test Game 2");
            CreateTestGameAndSaveIt("Test Game 3");
            List<SaveGameManager.SaveGameInfo> lastGames = gameManager.LastGamesInMooGameFolder;
            Assert.AreEqual(3, lastGames.Count, "There must be 3 saved games in the moo game folder");
            Assert.AreEqual("Test Game 1", lastGames[0].GameName);
            Assert.AreEqual("Test Game 2", lastGames[1].GameName);
            Assert.AreEqual("Test Game 3", lastGames[2].GameName);
        }

        /// <summary>
        /// Creates the test game and stores it in the game folder.
        /// </summary>
        /// <param name="gameName">Name of the game.</param>
        private static void CreateTestGameAndSaveIt(string gameName)
        {
            var gameManager = new SaveGameManager(true);
            SaveGame game = TestDataGenerator.TestSaveGame;
            gameManager.Save(game, gameName);
        }


        /// <summary>
        /// Tests the member function SaveGameManager.GetDefaultFilename().
        /// </summary>
        [TestMethod]
        public void TestGetDefaultName()
        {
            var gameSettings = new GameSettings {EmpireName = "Test Empire"};
            var gameGenerator = new GameGenerator(gameSettings);
            var game = gameGenerator.GenerateNewGame();
            var defaultGameName = new SaveGameManager(true).GetDefaultFilename(game);
            Assert.AreEqual("Test Empire - 000", defaultGameName);
        }
    }
}